//
//  SPCard.m
//  ShandongPoker
//
//  Created by Developer on 2025-01-08.
//  Copyright © 2025 ShandongPoker. All rights reserved.
//

#import "SPCard.h"

@implementation SPCard

#pragma mark - Initialization

- (instancetype)initWithSuit:(SPCardSuit)suit rank:(SPCardRank)rank {
    if (self = [super init]) {
        _suit = suit;
        _rank = rank;
        _value = [self calculateValue];
    }
    return self;
}

#pragma mark - Private Methods

- (NSInteger)calculateValue {
    // A在德州扑克中可以作为1或14，这里统一作为14处理
    if (self.rank == SPCardRankAce) {
        return 14;
    }
    return self.rank;
}

#pragma mark - Public Methods

- (BOOL)isRed {
    return (self.suit == SPCardSuitHearts || self.suit == SPCardSuitDiamonds);
}

- (BOOL)isBlack {
    return (self.suit == SPCardSuitSpades || self.suit == SPCardSuitClubs);
}

- (NSString *)displayName {
    NSString *suitName = [self suitName];
    NSString *rankName = [self rankName];
    return [NSString stringWithFormat:@"%@%@", suitName, rankName];
}

- (NSString *)shortName {
    NSString *suitSymbol = [self suitSymbol];
    NSString *rankSymbol = [self rankSymbol];
    return [NSString stringWithFormat:@"%@%@", rankSymbol, suitSymbol];
}

- (NSString *)suitSymbol {
    switch (self.suit) {
        case SPCardSuitSpades:
            return @"♠";
        case SPCardSuitHearts:
            return @"♥";
        case SPCardSuitDiamonds:
            return @"♦";
        case SPCardSuitClubs:
            return @"♣";
    }
}

- (NSString *)rankSymbol {
    switch (self.rank) {
        case SPCardRankAce:
            return @"A";
        case SPCardRankKing:
            return @"K";
        case SPCardRankQueen:
            return @"Q";
        case SPCardRankJack:
            return @"J";
        case SPCardRankTen:
            return @"10";
        default:
            return [NSString stringWithFormat:@"%ld", (long)self.rank];
    }
}

- (NSString *)suitName {
    switch (self.suit) {
        case SPCardSuitSpades:
            return @"黑桃";
        case SPCardSuitHearts:
            return @"红桃";
        case SPCardSuitDiamonds:
            return @"方块";
        case SPCardSuitClubs:
            return @"梅花";
    }
}

- (NSString *)rankName {
    switch (self.rank) {
        case SPCardRankAce:
            return @"A";
        case SPCardRankKing:
            return @"K";
        case SPCardRankQueen:
            return @"Q";
        case SPCardRankJack:
            return @"J";
        default:
            return [NSString stringWithFormat:@"%ld", (long)self.rank];
    }
}

- (UIImage *)cardImage {
    NSString *imageName = [NSString stringWithFormat:@"card_%@_%ld", 
                          [self suitImageName], (long)self.rank];
    return [UIImage imageNamed:imageName];
}

+ (UIImage *)cardBackImage {
    return [UIImage imageNamed:@"card_back"];
}

- (NSString *)suitImageName {
    switch (self.suit) {
        case SPCardSuitSpades:
            return @"spades";
        case SPCardSuitHearts:
            return @"hearts";
        case SPCardSuitDiamonds:
            return @"diamonds";
        case SPCardSuitClubs:
            return @"clubs";
    }
}

- (NSComparisonResult)compare:(SPCard *)otherCard {
    if (self.value < otherCard.value) {
        return NSOrderedAscending;
    } else if (self.value > otherCard.value) {
        return NSOrderedDescending;
    } else {
        return NSOrderedSame;
    }
}

#pragma mark - NSObject

- (BOOL)isEqual:(id)object {
    if (![object isKindOfClass:[SPCard class]]) {
        return NO;
    }
    
    SPCard *otherCard = (SPCard *)object;
    return (self.suit == otherCard.suit && self.rank == otherCard.rank);
}

- (NSUInteger)hash {
    return self.suit * 100 + self.rank;
}

- (NSString *)description {
    return [self displayName];
}

#pragma mark - NSCopying

- (id)copyWithZone:(NSZone *)zone {
    return [[SPCard alloc] initWithSuit:self.suit rank:self.rank];
}

@end